﻿Public Class clsWydrukZlecenia

    Private mvarDbHolder As clsDataBase
    Private mvarPageCount As Integer
    Private mvarCurrentPage As Integer
    Private mvarIdLine As Integer = 1
    Private mvarZlecenieNew As clsInhZlecenie
    Private mvarTmpI As Integer = 1
    Private mvarIdZlecenia As Integer

    Public Sub New(ByVal IdZlecenia As Integer)

        mvarDbHolder = New clsDataBase(VAR_CONNECTION_PARAMETERS, VAR_ERROR_HANDLER)

        Dim print_document As System.Drawing.Printing.PrintDocument = PreparePrintDocument()

        mvarZlecenieNew = New clsInhZlecenie(IdZlecenia, mvarDbHolder)
        '     print_document.PrinterSettings.PrinterName = "TicketPrinter"

        mvarCurrentPage = 1
        print_document.Print()
        mvarDbHolder.Close()

    End Sub

    Private Sub Print_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

        Dim lvarFontNormal = New System.Drawing.Font("Arial", 9, FontStyle.Regular)
        Dim lvarFontNaglowek = New System.Drawing.Font("Arial", 13, FontStyle.Bold)
        Dim lvarFontNaglowekiTabel = New System.Drawing.Font("Arial", 9, FontStyle.Bold)
        Dim lvarFontData = New System.Drawing.Font("Arial", 8, FontStyle.Regular)
        Dim lvarFontNumerZlecenia = New System.Drawing.Font("Arial", 20, FontStyle.Bold)
        Dim lvarFontKodZlecenia = New System.Drawing.Font("Code 128", 50, FontStyle.Regular)
        Dim lvarFontKodPozycji = New System.Drawing.Font("Code 128", 20, FontStyle.Regular)
        Dim lvarFontInformacje = New System.Drawing.Font("Arial", 7, FontStyle.Italic)
        Dim lvarFontTmp As System.Drawing.Font

        Dim lvarNumerKoduZlecenia As String
        Dim lvarNumerKoduPozycji As String

        Dim lvarRecDataWydruku As System.Drawing.Rectangle
        Dim lvarRecNazwaNumer As System.Drawing.Rectangle
        Dim lvarRecNumerZlecenia As System.Drawing.Rectangle
        Dim lvarRecKodZlecenia As System.Drawing.Rectangle

        Dim lvarPointRamki As New System.Drawing.PointF
        Dim lvarPoint As New System.Drawing.PointF

        Dim lvarStringFormat As New System.Drawing.StringFormat

        'Data i godzina wydruku
        With e.Graphics

            lvarPoint.X = CSng(e.PageSettings.Bounds.Width / 2 + 25)
            lvarPoint.Y = 25
            lvarStringFormat.Alignment = StringAlignment.Far

            'napis 
            lvarRecDataWydruku = New System.Drawing.Rectangle(CInt(lvarPoint.X), CInt(lvarPoint.Y), 110, 13)
            .DrawString("DATA WYDRUKU: ", lvarFontData, Brushes.Black, lvarRecDataWydruku, lvarStringFormat)
            '.DrawRectangle(Pens.Black, lvarRecDataWydruku)

            'data
            lvarPoint.X += 110
            lvarStringFormat.Alignment = StringAlignment.Near
            lvarRecDataWydruku = New System.Drawing.Rectangle(CInt(lvarPoint.X), CInt(lvarPoint.Y), 180, 13)
            .DrawString(Now.ToString(), lvarFontData, Brushes.Black, lvarRecDataWydruku, lvarStringFormat)
            ' .DrawRectangle(Pens.Black, lvarRecDataWydruku)

        End With

        lvarNumerKoduZlecenia = BarCode.BarcodeConverter128.StringToBarcode(mvarZlecenieNew.Numer)
        lvarStringFormat.Alignment = StringAlignment.Center

        lvarPoint.Y = e.MarginBounds.X
        lvarPoint.X = e.MarginBounds.X

        With e.Graphics

            'NazwaNumer
            lvarStringFormat.Alignment = StringAlignment.Center
            lvarStringFormat.LineAlignment = StringAlignment.Center
            lvarRecNazwaNumer = New System.Drawing.Rectangle(CInt(lvarPoint.X), CInt(lvarPoint.Y), 200, 100)
            .DrawString("NUMER ZLECENIA: ", lvarFontNaglowek, Brushes.Black, lvarRecNazwaNumer, lvarStringFormat)
            '.DrawRectangle(Pens.Black, lvarRecNazwaNumer)

            'Numer
            lvarPoint.X += 200
            lvarStringFormat.Alignment = StringAlignment.Near
            lvarRecNumerZlecenia = New System.Drawing.Rectangle(CInt(lvarPoint.X), CInt(lvarPoint.Y), 160, 100)
            .DrawString(mvarZlecenieNew.Numer, lvarFontNumerZlecenia, Brushes.Black, lvarRecNumerZlecenia, lvarStringFormat)
            '.DrawRectangle(Pens.Black, lvarRecNumer)

            'Kod
            lvarPoint.X += 160
            lvarRecKodZlecenia = New System.Drawing.Rectangle(CInt(lvarPoint.X), CInt(lvarPoint.Y), 250, 100)
            .DrawString(lvarNumerKoduZlecenia, lvarFontKodZlecenia, Brushes.Black, lvarRecKodZlecenia, lvarStringFormat)
            ' .DrawRectangle(Pens.Black, lvarRecKod)

        End With


        'Linie zlecenia

        'naglowki tabeli
        Dim lvarNaglowekTabeli As New System.Drawing.PointF
        Dim lvarLp As New System.Drawing.PointF
        Dim lvarNumer As New System.Drawing.PointF
        Dim lvarMaterial As New System.Drawing.PointF
        Dim lvarIlosc As New System.Drawing.PointF
        Dim lvarPakowanie As New System.Drawing.PointF
        Dim lvarUwagi As New System.Drawing.PointF
        Dim lvarInformacje As New System.Drawing.PointF
        Dim lvarKodPozycji As New System.Drawing.PointF

        Dim lvarRecTabela As System.Drawing.Rectangle
        Dim lvarRecLp As System.Drawing.Rectangle
        Dim lvarRecNumer As System.Drawing.Rectangle
        Dim lvarRecMaterial As System.Drawing.Rectangle
        Dim lvarRecIlosc As System.Drawing.Rectangle
        Dim lvarRecPakowanie As System.Drawing.Rectangle
        Dim lvarRecUwagi As System.Drawing.Rectangle
        Dim lvarRecInformacje As System.Drawing.Rectangle
        Dim lvarRecKodPozycji As System.Drawing.Rectangle

        lvarStringFormat.Alignment = StringAlignment.Center
        lvarStringFormat.LineAlignment = StringAlignment.Center

        lvarPoint.Y += 120
        lvarPoint.X = e.MarginBounds.X - 50
        lvarNaglowekTabeli = lvarPoint
        lvarLp = lvarPoint

        lvarPoint.X = lvarPoint.X + 27
        lvarNumer = lvarPoint
        lvarPoint.X = lvarPoint.X + 90
        lvarMaterial = lvarPoint
        lvarPoint.X = lvarPoint.X + 100
        lvarIlosc = lvarPoint
        lvarPoint.X = lvarPoint.X + 60
        lvarPakowanie = lvarPoint
        lvarPoint.X = lvarPoint.X + 100
        lvarUwagi = lvarPoint
        lvarPoint.X = lvarPoint.X + 250
        lvarKodPozycji = lvarPoint

        With e.Graphics
            lvarRecTabela = New System.Drawing.Rectangle(CInt(lvarNaglowekTabeli.X), CInt(lvarNaglowekTabeli.Y), e.MarginBounds.Width + 100, 20)
            .DrawRectangle(Pens.Black, lvarRecTabela)
            'Lp
            lvarRecLp = New System.Drawing.Rectangle(CInt(lvarLp.X), CInt(lvarLp.Y), 27, 20)
            .DrawString("Lp", lvarFontNaglowekiTabel, Brushes.Black, lvarRecLp, lvarStringFormat)
            '.DrawRectangle(Pens.Black, lvarRecLp)
            'Numer
            lvarRecNumer = New System.Drawing.Rectangle(CInt(lvarNumer.X), CInt(lvarNumer.Y), 90, 20)
            .DrawString("Numer", lvarFontNaglowekiTabel, Brushes.Black, lvarRecNumer, lvarStringFormat)
            '.DrawRectangle(Pens.Black, lvarRecMaterial)
            'Material
            lvarRecMaterial = New System.Drawing.Rectangle(CInt(lvarMaterial.X), CInt(lvarMaterial.Y), 100, 20)
            .DrawString("Materiał", lvarFontNaglowekiTabel, Brushes.Black, lvarRecMaterial, lvarStringFormat)
            '.DrawRectangle(Pens.Black, lvarRecMaterial)
            'Ilosc
            lvarRecIlosc = New System.Drawing.Rectangle(CInt(lvarIlosc.X), CInt(lvarIlosc.Y), 60, 20)
            .DrawString("Ilość", lvarFontNaglowekiTabel, Brushes.Black, lvarRecIlosc, lvarStringFormat)
            ' .DrawRectangle(Pens.Black, lvarRecIlosc)
            'Pakowanie
            lvarRecPakowanie = New System.Drawing.Rectangle(CInt(lvarPakowanie.X), CInt(lvarPakowanie.Y), 100, 20)
            .DrawString("Pakowanie", lvarFontNaglowekiTabel, Brushes.Black, lvarRecPakowanie, lvarStringFormat)
            ' .DrawRectangle(Pens.Black, lvarRecPakowanie)
            'Uwagi
            lvarRecUwagi = New System.Drawing.Rectangle(CInt(lvarUwagi.X), CInt(lvarUwagi.Y), 250, 20)
            .DrawString("Uwagi", lvarFontNaglowekiTabel, Brushes.Black, lvarRecUwagi, lvarStringFormat)
            '.DrawRectangle(Pens.Black, lvarRecUwagi)
            'Kod pozycji
            lvarRecKodPozycji = New System.Drawing.Rectangle(CInt(lvarKodPozycji.X), CInt(lvarKodPozycji.Y), 100, 20)
            .DrawString("Kod", lvarFontNaglowekiTabel, Brushes.Black, lvarRecKodPozycji, lvarStringFormat)
            '.DrawRectangle(Pens.Black, lvarRecUwagi)
        End With

        'wiersze tabeli
        lvarLp.Y = lvarLp.Y + 5
        lvarNumer.Y += 5
        lvarMaterial.Y += 5
        lvarIlosc.Y += 5
        lvarPakowanie.Y += 5
        lvarUwagi.Y += 5
        lvarKodPozycji.Y += 5

        Dim lvarI As Integer
        Dim lvarTekst As String
  
        For Each lvarPozycja As clsInhPozycjaZlecenia In mvarZlecenieNew.PozycjeZlecenia
            lvarI += 1
            lvarNumerKoduPozycji = BarCode.BarcodeConverter128.StringToBarcode(CStr(lvarPozycja.Id))

            lvarStringFormat.Alignment = StringAlignment.Center
            With e.Graphics
                'lp
                lvarLp.Y += 20
                lvarRecLp = New System.Drawing.Rectangle(CInt(lvarLp.X), CInt(lvarLp.Y), 27, 15)
                .DrawString(lvarI.ToString(), lvarFontNormal, Brushes.Black, lvarRecLp, lvarStringFormat)
                'numer
                lvarNumer.Y += 20
                lvarRecNumer = New System.Drawing.Rectangle(CInt(lvarNumer.X), CInt(lvarNumer.Y), 90, 15)
                .DrawString(lvarPozycja.Id.ToString(), lvarFontNormal, Brushes.Black, lvarRecNumer, lvarStringFormat)
                'material
                lvarMaterial.Y += 20
                lvarRecMaterial = New System.Drawing.Rectangle(CInt(lvarMaterial.X), CInt(lvarMaterial.Y), 100, 15)
                .DrawString(lvarPozycja.MaterialOgolnie.NazwaAng, lvarFontNormal, Brushes.Black, lvarRecMaterial, lvarStringFormat)
                'ilosc
                lvarIlosc.Y += 20
                lvarRecIlosc = New System.Drawing.Rectangle(CInt(lvarIlosc.X), CInt(lvarIlosc.Y), 60, 15)
                .DrawString(lvarPozycja.Ilosc & lvarPozycja.Jednostka.Jednostka, lvarFontNormal, Brushes.Black, lvarRecIlosc, lvarStringFormat)
                'pakowanie
                lvarPakowanie.Y += 20
                lvarRecPakowanie = New System.Drawing.Rectangle(CInt(lvarPakowanie.X), CInt(lvarPakowanie.Y), 100, 15)
                .DrawString(lvarPozycja.Pakowaie.Nazwa, lvarFontNormal, Brushes.Black, lvarRecPakowanie, lvarStringFormat)
                'uwagi
                lvarUwagi.Y += 20
                lvarRecUwagi = New System.Drawing.Rectangle(CInt(lvarUwagi.X), CInt(lvarUwagi.Y), 250, 15)
                lvarFontTmp = GetFont(lvarPozycja.Uwagi, lvarRecUwagi, e)
                .DrawString(lvarPozycja.Uwagi, lvarFontTmp, Brushes.Black, lvarRecUwagi, lvarStringFormat)
                '.DrawRectangle(Pens.Black, lvarRecUwagi)
                'kod zlecenia
                lvarKodPozycji.Y += 20
                lvarRecKodPozycji = New System.Drawing.Rectangle(CInt(lvarKodPozycji.X), CInt(lvarKodPozycji.Y), 100, 35)
                .DrawString(lvarNumerKoduPozycji, lvarFontKodPozycji, Brushes.Black, lvarRecKodPozycji, lvarStringFormat)
                '.DrawRectangle(Pens.Black, lvarRecKodPozycji)
                'informacje
                lvarNumer.Y += 15
                lvarInformacje = lvarNumer
                lvarStringFormat.Alignment = StringAlignment.Near
                lvarRecInformacje = New System.Drawing.Rectangle(CInt(lvarInformacje.X), CInt(lvarInformacje.Y), e.MarginBounds.Width - 27, 20)
                lvarTekst = "Wymiar: " & lvarPozycja.Wymiar & "; Tol.Dolna:" & lvarPozycja.TolDolna & "; Tol.Górna:" & lvarPozycja.TolGorna & "; Wydł.:" & lvarPozycja.Wydluzenie & "; Wytrz.Min:" & lvarPozycja.WytrzymaloscMin & "; Wytrz.Max:" & lvarPozycja.WytrzymaloscMax '& "; Świad.:" & lvarPozycjeZlecenia.Swiadectwo
                .DrawString(lvarTekst, lvarFontInformacje, Brushes.Black, lvarRecInformacje, lvarStringFormat)
                '.DrawRectangle(Pens.Black, lvarRecInformacje)
                lvarLp.Y = lvarLp.Y + 20
                lvarNumer.Y += 5
                lvarMaterial.Y += 20
                lvarIlosc.Y += 20
                lvarPakowanie.Y += 20
                lvarUwagi.Y += 20
                lvarKodPozycji.Y += 20

                mvarIdLine = mvarIdLine + 1
                ' mvarTmpI = lvarI
            End With

            mvarIdLine = mvarIdLine + 1
            mvarTmpI = lvarI

            'lvarTmp = lvarTmp + 1
            'If lvarTmp > 20 Then
            '    mvarTmpI += 1
            '    Exit For
            'End If

        Next

        'With e.Graphics
        '    lvarSuma.X = e.PageSettings.Bounds.Width / 3
        '    lvarSuma.Y = lvarLp.Y + 20
        '    .DrawLine(Pens.Black, lvarSuma.X, lvarSuma.Y + 20, lvarSuma.X + CInt(e.MarginBounds.Width / 2), lvarSuma.Y + 20)

        '    lvarSuma.X = lvarInformacje.X + 20
        '    lvarSuma.Y = lvarInformacje.Y + 40
        '    lvarRecSuma = New System.Drawing.Rectangle(lvarSuma.X, lvarSuma.Y, 100, 15)
        '    ' .DrawRectangle(Pens.Black, lvarRecSuma)
        '    .DrawString("Suma", lvarFontNormal, Brushes.Black, lvarRecSuma, lvarStringFormat)

        '    lvarRecSumaWartosc = New System.Drawing.Rectangle(lvarSuma.X + 100, lvarSuma.Y, 100, 15)
        '    ' .DrawRectangle(Pens.Black, lvarRecSumaWartosc)
        '    .DrawString(lvarWartoscSumy.ToString, lvarFontNormal, Brushes.Black, lvarRecSumaWartosc, lvarStringFormat)

        'End With




        'Numer strony

        'mvarCurrentPage = mvarCurrentPage + 1

        'lvarDotPen.Dispose()

        'If mvarCurrentPage <= mvarPageCount Then
        '    e.HasMorePages = True
        'Else
        '    e.HasMorePages = False
        'End If

    End Sub

    Private Function GetFont(ByVal Text As String, ByVal Rectangle As System.Drawing.Rectangle, ByVal e As System.Drawing.Printing.PrintPageEventArgs) As System.Drawing.Font

        Dim lvarFont As New System.Drawing.Font("Arial", 10)
        Dim lvarTextSize As SizeF
        Dim lvarFontSize As Integer = 8

        While (True)
            lvarFont = New System.Drawing.Font("Arial", lvarFontSize, FontStyle.Regular)
            lvarTextSize = e.Graphics.MeasureString(Text, lvarFont)

            If lvarTextSize.Width < Rectangle.Width Then
                Exit While
            End If
            lvarFontSize = lvarFontSize - 1
        End While

        Return lvarFont

    End Function

    Private Function PreparePrintDocument() As System.Drawing.Printing.PrintDocument
        ' Make the PrintDocument object.
        Dim print_document As New System.Drawing.Printing.PrintDocument

        ' Install the PrintPage event handler.
        AddHandler print_document.PrintPage, AddressOf Print_PrintPage

        ' Return the object.
        Return print_document

    End Function

End Class
